package com.itheima.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public Result add(CheckItem checkItem) {
        checkItemMapper.save(checkItem);
        return Result.success(MessageConstant.ADD_CHECKITEM_SUCCESS, null);
    }

    @Override
    public Result edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
        return Result.success(MessageConstant.EDIT_CHECKITEM_SUCCESS, null);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    @Override
    public Result delete(Integer id) {
        //查询当前检查项是否和检查组关联
        int count = checkItemMapper.findCountByCheckItemId(id);
        if (count > 0) {
            //当前检查项被引用，不能删除
            return Result.fail("当前检查项被引用，不能删除");
        }
        checkItemMapper.deleteById(id);
        return Result.success(MessageConstant.DELETE_CHECKITEM_SUCCESS, null);
    }

    @Override
    public Result pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemMapper.selectByCondition(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    @Override
    public Result findAll() {
        Jedis jedis = jedisPool.getResource();
        List<CheckItem> items = null;
        try {
            String checkItemStr = jedis.get("checkItem");
            if (StringUtils.isEmpty(checkItemStr)) {
                List<CheckItem> list = checkItemMapper.findAll();
                jedis.set("checkItem", JSON.toJSONString(list));
                System.out.println("从数据库获取数据");
                items = list;
            } else {
                System.out.println("从redis获取数据");
                items = JSON.parseArray(checkItemStr, CheckItem.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) jedis.close();
        }
        return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, items);
//        return checkItemMapper.findAll();
    }
}
